// Copyright (c) 2011, Jerzy Chalupski
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//     * Redistributions of source code must retain the above copyright
//       notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above copyright
//       notice, this list of conditions and the following disclaimer in the
//       documentation and/or other materials provided with the distribution.
//     * Neither the name of the Jerzy Chalupski nor the
//       names of its contributors may be used to endorse or promote products
//       derived from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL JERZY CHALUPSKI BE LIABLE FOR ANY
// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

import Qt 4.7

Rectangle {
    id: root
    width: 500
    height: 400

    color: "#EEEEEE"

    Item {
        id: rabbit
        width: childrenRect.width
        height: childrenRect.height

        OldschoolLcd {
            id: rabbitBody
            imageSource: "graphics/rabbit.png"
            lit: rabbitVisibility || displayBellAnimation
        }

        OldschoolLcd {
            id: bellup
            imageSource: "graphics/bellup.png"
            anchors.left: rabbitBody.right
            anchors.top: rabbitBody.top
            anchors.topMargin: 10
        }

        OldschoolLcd {
            id: belldown
            imageSource: "graphics/belldown.png"
            anchors.left: rabbitBody.right
            anchors.top: bellup.bottom
        }

        states: [
            State {
                name: "ringTheBell"
                when: displayBellAnimation
            }
        ]

        transitions: Transition {
             to: "ringTheBell"
             SequentialAnimation {
                 PropertyAction { target: bellup; property: "lit"; value: true; }
                 PropertyAction { target: belldown; property: "lit"; value: false; }
                 PauseAnimation { duration: 20 * tick }

                 PropertyAction { target: bellup; property: "lit"; value: false; }
                 PropertyAction { target: belldown; property: "lit"; value: true; }
                 PauseAnimation { duration: 20 * tick }

                 PropertyAction { target: bellup; property: "lit"; value: true; }
                 PropertyAction { target: belldown; property: "lit"; value: false; }
                 PauseAnimation { duration: 20 * tick }

                 PropertyAction { target: bellup; property: "lit"; value: false; }
                 PropertyAction { target: belldown; property: "lit"; value: true; }
                 PauseAnimation { duration: 20 * tick }

                 PropertyAction { target: bellup; property: "lit"; value: false; }
                 PropertyAction { target: belldown; property: "lit"; value: false; }
                 ScriptAction { script: bellAnimationFinished() }
             }
        }

        anchors.horizontalCenter: leftRoosts.right
        anchors.horizontalCenterOffset: 10
        anchors.top: leftRoosts.top
    }

    LcdCounter {
        id: scoreDisplay
        value: score

        anchors.left: rightWolf.left
        anchors.top: rabbit.top
    }

    Item {
        id: leftWolf

        width: childrenRect.width
        height: childrenRect.height

        OldschoolLcd {
            id: topLeftBasket
            imageSource: "graphics/baskettopleft.png"
            anchors.left: leftWolf.left
            anchors.top: leftWolf.top
            lit: wolfPosition == 0
        }

        OldschoolLcd {
            imageSource: "graphics/basketbottomleft.png"
            anchors.left: leftWolf.left
            anchors.top: topLeftBasket.bottom
            lit: wolfPosition == 1
        }


        OldschoolLcd {
            id: wolfBodyLeft
            imageSource: "graphics/wolfleft.png"
            anchors.left: topLeftBasket.right
            anchors.leftMargin: -20
            anchors.top: leftWolf.top
            lit: wolfPosition == 0 || wolfPosition == 1
        }

        Overlay {
            imageSource: "graphics/busheswolfleft.png"
            anchors.left: wolfBodyLeft.left
            anchors.verticalCenter: wolfBodyLeft.bottom
        }

        anchors.left: leftRoosts.right
        anchors.bottom: leftRoosts.bottom
        anchors.bottomMargin: -40
    }

    Item {
        id: rightWolf

        width: childrenRect.width
        height: childrenRect.height

        OldschoolLcd {
            id: topRightBasket
            imageSource: "graphics/baskettopright.png"
            anchors.right: rightWolf.right
            anchors.top: rightWolf.top
            anchors.topMargin: 5
            lit: wolfPosition == 2
        }

        OldschoolLcd {
            imageSource: "graphics/basketbottomright.png"
            anchors.right: rightWolf.right
            anchors.top: topRightBasket.bottom
            anchors.topMargin: 5
            lit: wolfPosition == 3
        }


        OldschoolLcd {
            id: wolfBodyRight
            imageSource: "graphics/wolfright.png"
            anchors.right: topRightBasket.left
            anchors.rightMargin: -5
            anchors.top: rightWolf.top
            lit: wolfPosition == 2 || wolfPosition == 3
        }

        Overlay {
            imageSource: "graphics/busheswolfright.png"
            anchors.horizontalCenter: wolfBodyRight.horizontalCenter
            anchors.verticalCenter: wolfBodyRight.bottom
        }

        anchors.right: rightRoosts.left
        anchors.rightMargin: 15
        anchors.bottom: rightRoosts.bottom
        anchors.bottomMargin: -40
    }

    Item {
        id: leftRoosts
        width: childrenRect.width
        height: childrenRect.height

        Overlay {
            id: leftRoostOverlay
            imageSource: "graphics/leftroostsoverlay.png"
        }

        EggRow {
            id: topLeftEggs
            eggState: eggsState & 0x000F
            animate: root.state == "game"

            anchors.top: leftRoostOverlay.bottom
            anchors.topMargin: -178
            anchors.right: leftRoostOverlay.right
            anchors.rightMargin: -26
        }

        EggRow {
            id: bottomLeftEggs
            eggState: (eggsState & 0x00F0) >> 4
            animate: root.state == "game"

            anchors.top: leftRoostOverlay.bottom
            anchors.topMargin: -93
            anchors.right: leftRoostOverlay.right
            anchors.rightMargin: -26
        }

        anchors.left: parent.left
        anchors.bottom: rightRoosts.bottom
    }

    Item {
        id: rightRoosts
        width: childrenRect.width
        height: childrenRect.height

        Overlay {
            id: rightRoostOverlay
            imageSource: "graphics/rightroostsoverlay.png"
        }

        EggRow {
            id: topRightEggs
            eggState: (eggsState & 0x0F00) >> 8
            animate: root.state == "game"

            transform: Rotation { origin.x: topRightEggs.x - 13 + topRightEggs.width / 2; axis { x: 0; y: 1; z: 0 } angle: 180 }
            anchors.top: rightRoostOverlay.bottom
            anchors.topMargin: -178
        }

        EggRow {
            id: bottomRightEggs
            eggState: (eggsState & 0xF000) >> 12
            animate: root.state == "game"

            transform: Rotation { origin.x: bottomRightEggs.x - 13 + bottomRightEggs.width / 2; axis { x: 0; y: 1; z: 0 } angle: 180 }
            anchors.top: rightRoostOverlay.bottom
            anchors.topMargin: -93
        }

        anchors.right: parent.right
        anchors.top: parent.top
    }

    RunningChicken {
        id: leftChicken

        anchors.left: parent.left
        anchors.top: leftRoosts.bottom
        anchors.topMargin: 10

        chickenLit: brokenEggPosition == -1 && displayChicken
        eggLit: brokenEggPosition == -1 && displayBrokenEgg
    }

    RunningChicken {
        id: rightChicken

        transform: Rotation { origin.x: rightChicken.width / 2; axis { x: 0; y: 1; z: 0 } angle: 180 }

        anchors.right: parent.right
        anchors.top: rightRoosts.bottom
        anchors.topMargin: 10

        chickenLit: brokenEggPosition == 1 && displayChicken
        eggLit: brokenEggPosition == 1 && displayBrokenEgg
    }

    LifeCounter {
        id: lives

        broken: brokenEggs

        anchors.top: scoreDisplay.bottom
        anchors.topMargin: 30
        anchors.left: scoreDisplay.left
    }

    Item {
        id: touchScreen
        anchors.fill: parent

        Grid {
            columns: 2
            rows: 2

            Repeater {
                model: [0, 2, 1, 3]
                MouseArea { width: touchScreen.width / 2; height: touchScreen.height / 2; onClicked: wolfPosition = modelData }
            }
        }
    }


    focus: true
    Keys.onPressed: {
        switch (event.key) {
            case Qt.Key_A: wolfPosition = 0; event.accepted = true; break;
            case Qt.Key_Z: wolfPosition = 1; event.accepted = true; break;
            case Qt.Key_Apostrophe: wolfPosition = 2; event.accepted = true; break;
            case Qt.Key_Slash: wolfPosition = 3; event.accepted = true; break;
            default: event.accepted = false;
        }
    }

    states: [
        State {
            name: "game"
            when: gameStarted
        }
    ]

    Column {
        id: buttons
        spacing: 25
        anchors.centerIn: parent

        visible: !gameStarted

        Button {
            id: newGameButton
            caption: "New game"
            onClicked: { newGameClicked() }
        }
        Button {
            id: quitButton
            caption: "Quit"
            onClicked: Qt.quit()
        }
    }
}
